﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Common.Service.DataTables;
using SMECustomerWebsite.Core.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class AdminHrController : ControllerBase
    {
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult EditMSBO(string dao)
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ViewAllData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                filter.ContractType = pageRequest.SearchColumns[4];
                filter.Email = pageRequest.SearchColumns[5];
                filter.CusOpenDate = pageRequest.SearchColumns[6];

                RetrieveListMsboRequest request = new RetrieveListMsboRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.MSBOController.ViewAllData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveListMsboResponse();
                customerRecords = serviceClient.Proxy.RetrieveListMsbo(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<UserRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                                 x.Zone,
                                                                 x.Branch,
                                                                 x.Dao,
                                                                 x.FullName,
                                                                 x.ContractType,
                                                                 x.Email,
                                                                 x.StartWorkingDate,
                                                                 x.Dao
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }
    }
}